package com.sky.controller.admin;


import com.sky.annotation.Log;
import com.sky.entity.Category;
import com.sky.enumeration.BusinessType;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController("adminCategoryController")
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    // 根据分类查询
    @GetMapping("/list")
    public Result listCategory(String type) {
        List<Category> listCategory = categoryService.listCategory(type);
        return Result.success(listCategory);
    }

    // 添加分类

    @PostMapping
    @Log(title = "添加分类",businessType = BusinessType.INSERT)
    public Result addCategory(@RequestBody Category category) {
        log.info("添加分类");
        categoryService.addCategory(category);
        return Result.success();

    }

    // 根据ID修改分类信息
    @PutMapping
    public Result getCategory(@RequestBody Category category) {
        categoryService.updateCategory(category);
        return Result.success();
    }

    // 删除分类信息
    @DeleteMapping
    public Result deleteCategory(String id) {
        categoryService.deleteCategory(id);
        return Result.success();
    }

    // 分页查询
    @GetMapping("/page")
    public Result getPage(String page,String pageSize,String name,String type){
        PageBean pageBean=categoryService.getPage(page,pageSize ,name,type);
        return Result.success(pageBean);
    }

    //启用与禁止
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable String status,Long id){
        categoryService.updateSatus(status,id);
        return Result.success();
    }
}
